//科目设置控制器。
Ext.define("ES.controller.CatalogController",{
    extend:"Ext.app.Controller",
    models:["CatalogModel","NotSelfModel"],
    stores:["CatalogStore","NotSelfCatalogStore"],
    views:["catalog.CatalogList","catalog.CatalogEdit"],
    init:function(){
        this.control({
            "CatalogTabList":{
                itemdblclick:this.onEdit
            },
            "CatalogTabList button[action=btnQuery]":{
                click:this.onSearch
            }, 
            "CatalogTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "CatalogTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "catalogEdit button[action=btnSave]":{
                click:this.onSave
            },
            "catalogEdit button[action=btnCancel]":{
                click:function(btn){
                     var win = btn.up("catalogEdit");
                     if(win){
                         win.close();
                     }
                }
            }
        });
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.load({
            params:{
                catalogName:toolbar.getComponent(1).getValue()
            }
        });
    },
    onAdd:function(){
        var win = Ext.widget("catalogEdit");
        if(win){
            var st = this.getNotSelfCatalogStoreStore();
            st.load({
                params:{catalogID:""}
            });
            win.show();
        }
    },
    onEdit:function(view, record, item,index,e,opt){
        var win =Ext.widget("catalogEdit");
        if(win){   
            var st = this.getNotSelfCatalogStoreStore();
            win.show();
            var el = win.getEl(); 
            el.mask("正在加载数据，请稍后....");
            if(record){
                st.load({
                     params:{catalogID:record.get("catalogID")}
                 });
                var frm = win.down("form");
                if(frm){
                    frm.loadRecord(record);
                }
            }
            el.unmask();
        }
    },
    onSave:function(btn){
        var win = btn.up("catalogEdit");
        var form = btn.up("form").getForm();
        if(form.isValid()){
            var store = this.getCatalogStoreStore();
            var record = form.getRecord();
            var values = form.getValues();
            if(record){
                if(typeof(values.catalogType) == "undefined"){
                    record.set("catalogType", 0);
                }
                record.set(values);
            }else{
                record = this.getCatalogModelModel().create();
                record.set(values);
                store.insert(0,record);
            }
            store.sync();
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                        Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                        var st = grid.getStore();
                        Ext.Array.each(data, function(record){
                            st.remove(record);
                        })
                        st.sync();
                }
            }
        });
    }
})